#!user/bin/python
# coding: utf8
from openpyxl import Workbook
class ParserUtil:
    month_guide = {"Jan": "1", "Feb": "2", "Mar": "3", "Apr": "4", "May": "5", "Jul": "6",
                   "Jun": "7", "Aug": "8", "Sep": "9", "Oct": "10", "Nov": "11", "Dec": "12"}
    day_guide = {"1st": "1", "2nd": "2", "3rd": '3', "4th": "4", "5th": "5", "6th": "6", "7th": "7", "8th": "8",
                 "9th": "9", "10th": "10", "11th": "11", "12th": "12", "13th": "14", "14th": "14", "15th": "15",
                 "16th": "16", "17th": "17", "18th": "18", "19th": "19", "20th": "20", "21st": "20", "22nd": "22",
                 "23rd": "23", "24th": "24", "25th": "25", "26th": "26", "27th": "27", "28th": "28",
                 "29th": "29", "30th": "30", "31st": "31"}

    def parseDate(self,old_date):
        ''''''
        date = old_date.split(' ')
        year = date[2]
        month = self.month_guide[date[1]]
        day = self.day_guide[date[0]]
        newdate = year + '.' + month + '.' + day
        return newdate

    def exportExcel(self,datalist,filename):
        ''''''
        wb = Workbook()
        ws = wb.active
        ws['A1'] = u'id'
        ws['B1'] = u'title'
        ws['C1'] = u'href'
        ws['D1'] = u'source'
        ws['E1'] = u'publish_date'
        ws['F1'] = u'record_date'
        ws['G1'] = u'keywords'
        ws['H1'] = u'text'
        ws['I1'] = u'isdown'
        ws['J1'] = u'modify_time'
        i = 1
        for each in datalist:
            print u'processing row:%d' %(i)
            i += 1
            ws['A'+str(i)] = each[0]
            ws['B'+str(i)] = each[1]
            ws['C'+str(i)] = each[2]
            ws['D'+str(i)] = each[3]
            ws['E'+str(i)] = each[4]
            ws['F'+str(i)] = each[5]
            ws['G'+str(i)] = each[6]
            ws['H'+str(i)] = each[7]
            ws['I'+str(i)] = each[8]
            ws['J'+str(i)] = each[9]
        wb.save(filename)
        print u"total.xlsx has been created in folder file"
